using AjeeleeCms.Core.Common;
using AjeeleeCms.Web.Plugin.Link.BLL;
using AjeeleeCms.Web.Plugin.Link.Model;
using AjeeleeCms.Web.UI;
using System;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace AjeeleeCms.Web.Plugin.Link.admin
{
	public class index : ManagePage
	{
		protected int totalCount;
		protected int page;
		protected int pageSize;
		protected string keywords = "";
		protected HtmlForm form1;
		protected TextBox txtKeywords;
		protected Button btnSearch;
		protected LinkButton btnSave;
		protected LinkButton lbtnUnLock;
		protected LinkButton btnDelete;
		protected Repeater rptList;
		protected HtmlGenericControl PageContent;
		protected TextBox txtPageNum;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(base.Request.Params["keywords"]))
			{
				this.keywords = base.Request.Params["keywords"].Trim();
			}
			this.pageSize = this.GetPageSize(15);
			if (!this.Page.IsPostBack)
			{
				this.RptBind("id>0" + this.CombSqlTxt(this.keywords), "sort_id asc,add_time desc");
			}
		}
		private void RptBind(string _strWhere, string _orderby)
		{
			if (!int.TryParse(base.Request.QueryString["page"], out this.page))
			{
				this.page = 1;
			}
			this.txtKeywords.Text = this.keywords;
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			this.rptList.DataSource = link.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
			this.rptList.DataBind();
			this.txtPageNum.Text = this.pageSize.ToString();
			string text = Utils.CombUrlTxt("index.aspx", "keywords={0}&page={1}", new string[]
			{
				this.keywords, 
				"__id__"
			});
			this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, text, 8);
		}
		protected string CombSqlTxt(string _keywords)
		{
			StringBuilder stringBuilder = new StringBuilder();
			_keywords = _keywords.Replace("'", "");
			if (!string.IsNullOrEmpty(_keywords))
			{
				stringBuilder.Append(" and title like '%" + _keywords + "%'");
			}
			return stringBuilder.ToString();
		}
		private int GetPageSize(int _default_size)
		{
			int num;
			int result;
			if (int.TryParse(Utils.GetCookie("link_page_size"), out num))
			{
				if (num > 0)
				{
					result = num;
					return result;
				}
			}
			result = _default_size;
			return result;
		}
		protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			int id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidId")).Value);
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			AjeeleeCms.Web.Plugin.Link.Model.link model = link.GetModel(id);
			string text = e.CommandName.ToLower();
			if (text != null)
			{
				if (text == "ibtnred")
				{
					if (model.is_red == 1)
					{
						link.UpdateField(id, "is_red=0");
					}
					else
					{
						link.UpdateField(id, "is_red=1");
					}
				}
			}
			this.RptBind("id>0" + this.CombSqlTxt(this.keywords), "sort_id asc,add_time desc");
		}
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			base.Response.Redirect(Utils.CombUrlTxt("index.aspx", "keywords={0}", new string[]
			{
				this.txtKeywords.Text
			}));
		}
		protected void txtPageNum_TextChanged(object sender, EventArgs e)
		{
			int num;
			if (int.TryParse(this.txtPageNum.Text.Trim(), out num))
			{
				if (num > 0)
				{
					Utils.WriteCookie("link_page_size", num.ToString(), 43200);
				}
			}
			base.Response.Redirect(Utils.CombUrlTxt("index.aspx", "keywords={0}", new string[]
			{
				this.keywords
			}));
		}
		protected void btnSave_Click(object sender, EventArgs e)
		{
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			for (int i = 0; i < this.rptList.Items.Count; i++)
			{
				int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
				int num;
				if (!int.TryParse(((TextBox)this.rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out num))
				{
					num = 99;
				}
				link.UpdateField(id, "sort_id=" + num.ToString());
			}
			base.JscriptMsg("保存排序成功啦！", Utils.CombUrlTxt("index.aspx", "keywords={0}", new string[]
			{
				this.keywords
			}), "Success");
		}
		protected void lbtnUnLock_Click(object sender, EventArgs e)
		{
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			for (int i = 0; i < this.rptList.Items.Count; i++)
			{
				int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
				CheckBox checkBox = (CheckBox)this.rptList.Items[i].FindControl("chkId");
				if (checkBox.Checked)
				{
					link.UpdateField(id, "is_lock=0");
				}
			}
			base.JscriptMsg("批量审核成功啦！", Utils.CombUrlTxt("index.aspx", "keywords={0}", new string[]
			{
				this.keywords
			}), "Success");
		}
		protected void btnDelete_Click(object sender, EventArgs e)
		{
			AjeeleeCms.Web.Plugin.Link.BLL.link link = new AjeeleeCms.Web.Plugin.Link.BLL.link();
			for (int i = 0; i < this.rptList.Items.Count; i++)
			{
				int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
				CheckBox checkBox = (CheckBox)this.rptList.Items[i].FindControl("chkId");
				if (checkBox.Checked)
				{
					link.Delete(id);
				}
			}
			base.JscriptMsg("批量删除成功啦！", Utils.CombUrlTxt("index.aspx", "keywords={0}", new string[]
			{
				this.keywords
			}), "Success");
		}
	}
}
